﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using vensoft.Entities;
using System.Data;

namespace vensoft.DataAccess
{
    public class AlmacenDataAccess : MasterDataAcess, IAlmacenDataAccess
    {
        public AlmacenDataAccess()
            : base()
        { }

        public List<Almacen> GetAllFromAlmacen()
        {
            SqlCommand commando = base.CreateSelectCommand("GetAllFromAlmacen");
            return SelectObjectFactory(commando);
        }

        private List<Almacen> SelectObjectFactory(SqlCommand commando)
        {
            List<Almacen> lista = new List<Almacen>();

            using (SqlDataReader reader = base.ExecuteReader(commando))
            {
                while (reader.Read())
                {
                    Almacen alm = new Almacen()
                    {
                       ID_Almacen = reader.GetInt32(0),
                       Ubicacion = reader.GetString(1),
                       Descripcion = reader.GetString(2)
                    };

                    lista.Add(alm);
                }
            }

            return lista;
        }

        public void RegistrarAlmacen(Almacen almacen)
        {
            SqlCommand commando = base.CreateCommand("RegistrarAlmacen");
            AddParameters(almacen, commando);
        }

        private void AddParameters(Almacen almacen, SqlCommand command)
        {
            base.AddInParameter(command, "ID_Almacen", almacen.ID_Almacen);
            base.AddInParameter(command, "Ubicacion", almacen.Ubicacion);
            base.AddInParameter(command, "Descripcion", almacen.Descripcion);
            base.ExecuteCommand(command);
        }
        
        public void EliminarAlmacen(Almacen almacen)
        {
            SqlCommand command = base.CreateCommand("EliminarAlmacen");
            base.AddInParameter(command, "ID_Almacen", almacen.ID_Almacen);
            base.ExecuteCommand(command);
        }

        public List<Almacen> GetFromCategoriaByUbic(string criterio)
        {
            SqlCommand command = base.CreateSelectCommand("GetFromCategoriaByUbic");
            command.Parameters.AddWithValue("@Ubicacion", criterio);
            return SelectObjectFactory(command);
        }

        //public List<Almacen> GetProductosFromAlmacen(int codigo)
        //{
        //    SqlCommand command = base.CreateSelectCommand("GetProductosFromAlmacen");
        //    command.Parameters.AddWithValue("@cod_alm", codigo);
        //    //return SelectObjectFactory_ProdAlm(command);
        //}
        
        public List<Almacen> GetAllFromAlmacenByCodigo(int codigo)
        {
            SqlCommand commando = base.CreateSelectCommand("GetAllFromAlmacenByCodigo");
            commando.Parameters.AddWithValue("cod_alm", codigo);
            return SelectObjectFactory(commando);
        }

        public List<Almacen> RetornarAlmacenesOrigen(int codigo, string ubic)
        {
            SqlCommand commando = base.CreateSelectCommand("RetornarAlmacenesOrigen");
            commando.Parameters.AddWithValue("cod_alm", codigo);
            commando.Parameters.AddWithValue("ubic", ubic);
            return SelectObjectFactory(commando);
        }
      }
   }

